package com.android.nearshop.app.activity.main;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.activity.common.TemplateActivity;
import com.android.nearshop.app.adapter.listadapter.CommonListAdapter;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.business.model.FindCondition;
import com.android.nearshop.io.protocol.in.FindShopAction;
import com.android.nearshop.io.protocol.out.Area;
import com.android.nearshop.util.Constant;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-20
 * @see : 摇一摇结果
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class ShakeResultActivity extends TemplateActivity implements OnClickListener
{
    
    private CommonListAdapter mCommonListAdapter;
    private FindCondition mFindCondition;
    private FindShopAction mAction;
    private Button mFilter; 
    
    // 选中的城市
    private Area mArea;
    // 商品分类的选择
    private ArrayList<Integer> mCateList;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findshop);
        mFindCondition = (FindCondition)getIntent().getSerializableExtra(Constant.TAG_FIND_CONDITION);
        
        ListView listView = (ListView)findViewById(R.id.listView);
        mCommonListAdapter = new CommonListAdapter(this,listView);
        mCommonListAdapter.setIdisplayNetErrAndRefreshView(this);
        listView.setAdapter(mCommonListAdapter);
        
        mFilter = (Button) findViewById(R.id.sort);
        mFilter.setVisibility(View.VISIBLE); 
        mFilter.setOnClickListener(this);
        
        loadData();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        titleBar.setTitle(R.string.title_shake_result);
        titleBar.setRightVisible(false);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == FilterActivity.CODE_REQUEST)
        {
            if(resultCode == Activity.RESULT_OK)
            {
                mArea = (Area)data.getSerializableExtra(Constant.TAG_CITY);
                mCateList = data.getIntegerArrayListExtra(Constant.TAG_CATEGORYS);
                // 这里不做非法判断，非法就是移除了原来的条件，其实也是正确的[zhaohua]
                mAction.setAreaId(mArea == null ?  -1 : mArea.areaId);                                  
                mAction.setCatalogyIds(mCateList);
                
                mCommonListAdapter.resetPageInfo();
                mCommonListAdapter.reload();
                mCommonListAdapter.loadData();
            }
        }
    }
    
    @Override
    public void onClick(View v) 
    {
        switch(v.getId())
        {
            case R.id.sort:
            {
                FilterActivity.startActivityResult(this);
                break;
            }
        }
    }
    
    private void loadData()
    {        
        mAction = new FindShopAction(mFindCondition.lon, mFindCondition.lat, true, 1, 20);
        if(mFindCondition.areaId > 0)
        {
            mAction.setAreaId(mFindCondition.areaId);
        }
        if(mFindCondition.cateList != null && mFindCondition.cateList.size() > 0)
        {
            mAction.setCatalogyIds(mFindCondition.cateList);
        }
        mAction.setOrderStruct(true, false, false, false, false, false);
        mCommonListAdapter.setAction(mAction);
        mCommonListAdapter.setTotalView((TextView)findViewById(R.id.total_label));
        mCommonListAdapter.setToolView(findViewById(R.id.classic_button_panel));
        mCommonListAdapter.loadData();
    }
    
    public static void startActivity(Context context , FindCondition condition)
    {
        if(condition == null || !condition.isValid()) return;
        Intent it = new Intent(context, ShakeResultActivity.class);
        it.putExtra(Constant.TAG_FIND_CONDITION, condition);
        context.startActivity(it);
    }
}
